﻿//-----------------------------------------------------------------------
// <copyright file="PivotTableCategoryForm.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionMaker.WinFormApplication
{
    using System.Windows.Forms;
    using PivotCollectionTools.Data;
    using System;
    using PivotCollectionMaker.WinFormApplication.Controls;

    /// <summary>
    /// Category form.
    /// </summary>
    public partial class PivotTableCategoryForm : Form
    {
        /// <summary>
        /// Occurs before a category is deleted.
        /// </summary>
        public event EventHandler<CategoryListViewEventArgs> BeforeCategoryAdded;

        /// <summary>
        /// Flag for cancel operstaion;
        /// </summary>
        private bool cancel = false;

        /// <summary>
        /// Storage for the model
        /// </summary>
        private PivotTableCategory model;

        /// <summary>
        /// Initializes a new instance of the <see cref="PivotTableCategoryForm"/> class.
        /// </summary>
        public PivotTableCategoryForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model of the pivot table category form.</value>
        public PivotTableCategory Model 
        {
            get
            {
                return this.model;
            }

            set
            {
                this.model = value;
                this.BindData();
            }
        }

        /// <summary>
        /// Binds the data.
        /// </summary>
        private void BindData()
        {
            this.uxCategoryBindingSource.SuspendBinding();
            this.uxCategoryBindingSource.DataSource = this.model;
            this.uxCategoryBindingSource.ResumeBinding();

            this.uxValueType.DataSource = Enum.GetValues(typeof(PivotTableCellType));
            this.uxValueType.SelectedIndex = (int)this.model.ValueType;
        }

        private void PivotTableCategoryForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing || this.cancel)
            {
                return;
            }
            
            this.model.ValueType = (PivotTableCellType)this.uxValueType.SelectedItem;

            var listViewEventArgs = new CategoryListViewEventArgs(this.model, ListViewAction.Add);
            this.OnBeforeCategoryAdded(listViewEventArgs);
            if (listViewEventArgs.Continue == false)
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// Raises the <see cref="E:BeforeCategoryDeleted"/> event.
        /// </summary>
        /// <param name="e">The <see cref="PivotCollectionMaker.WinFormApplication.Controls.CategoryListViewEventArgs"/> instance containing the event data.</param>
        protected virtual void OnBeforeCategoryAdded(CategoryListViewEventArgs e)
        {
            if (this.BeforeCategoryAdded != null)
            {
                this.BeforeCategoryAdded(this, e);
            }
        }
        
        private void uxCancel_Click(object sender, EventArgs e)
        {
            this.cancel = true;
        }
    }
}
